//
//  RecruitmentDetailViewController.m
//  cityStar
//
//  Created by joyban on 15-1-21.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "RecruitmentDetailViewController.h"

#import "RecruitmentDetailCell.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"

NSString *const RecruitmentDetailCellID = @"recruitmentDetailCellID";

@interface RecruitmentDetailViewController ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
@property (weak, nonatomic) IBOutlet UILabel *addresslabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *timelabel;
@property (weak, nonatomic) IBOutlet UILabel *learnLabel;
@property (weak, nonatomic) IBOutlet UILabel *PositionLabel;
@property (weak, nonatomic) IBOutlet UILabel *companylabel;
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;
@property (weak, nonatomic) IBOutlet UILabel *workPathLabel;
@property (weak, nonatomic) IBOutlet UILabel *companyAddress;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *labelline;

@property (weak, nonatomic) IBOutlet UIButton *Consumptionbtn;
@property (weak, nonatomic) IBOutlet UITableView *RecruitmentDeatailTable;
@property (strong, nonatomic) IBOutlet UIView *heardView;

@end

@implementation RecruitmentDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"招聘详情";
    [self addBackButton];
    [self setdataHeardViewcontent];
    [self setUpTableViews];
    self.RecruitmentDeatailTable.tableHeaderView  = self.heardView;
    if([self.withchType isEqualToString:@"cityService"])
    {
        [self.Consumptionbtn setTitle:@"消费积分查看联系方式" forState:UIControlStateNormal];
    }else{
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateNormal];
        [self.Consumptionbtn setTitle:@"信息作废" forState:UIControlStateHighlighted];

        [self.Consumptionbtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    }
}

-(void)setdataHeardViewcontent
{
    self.titleLabel.text = self.recruitmentModel.title;
    NSInteger typestring = [[NSString stringWithFormat:@"%@",self.recruitmentModel.category] integerValue];
    switch (typestring) {
        case 1:
            self.typeLabel.text = @"全职";
            break;
        case 2:
            self.typeLabel.text = @"兼职";
            break;
        case 3:
            self.typeLabel.text = @"实习";
            break;
        default:
            self.typeLabel.hidden = YES;
            break;
    }
    self.addresslabel.text = self.recruitmentModel.address;
    NSInteger moneyStr = [[NSString stringWithFormat:@"%@",self.recruitmentModel.salary] integerValue];
    switch (moneyStr) {
        case 1:
            self.priceLabel.text = @"3000以下";
            break;
        case 2:
            self.priceLabel.text = @"3000-5000元/月";
            break;
        case 3:
            self.priceLabel.text = @"5000-8000元/月";
            break;
        case 4:
            self.priceLabel.text = @"8000-15000元/月";
            break;
        case 5:
            self.priceLabel.text = @"15000-20000元/月";
            break;
        case 6:
            self.priceLabel.text = @"20000+元/月";
            break;
        case 7:
            self.priceLabel.text = @"面议";
            break;
        default:
            break;
    }
    NSInteger leinStr = [[NSString stringWithFormat:@"%@",self.recruitmentModel.degree] integerValue];
    switch (leinStr) {
        case 0:
            self.learnLabel.text = @"不限";
            break;
        case 1:
            self.learnLabel.text = @"初中";
            break;
        case 2:
            self.learnLabel.text = @"高中";
            break;
        case 3:
            self.learnLabel.text = @"专专";
            break;
        case 4:
            self.learnLabel.text = @"大专";
            break;
        case 5:
            self.learnLabel.text = @"本科";
            break;
        case 6:
            self.learnLabel.text = @"硕士";
            break;
        case 7:
            self.learnLabel.text = @"博士";
            break;
        case 8:
            self.learnLabel.text = @"留学";
            break;
        default:
            break;
    }
    self.PositionLabel.text = self.recruitmentModel.career;
    self.companylabel.text = self.recruitmentModel.company_name;
    self.emailLabel.text = self.recruitmentModel.email;
    self.companyAddress.text = self.recruitmentModel.address;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 18)];
    label.font = [UIFont systemFontOfSize:15];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    NSString *_Detail = [NSString stringWithFormat:@"%@",self.companyAddress.text];
    label.text = _Detail;
    [label sizeToFit];
    float labelheight = label.frame.size.height;
    if(labelheight /15 > 2)
    {
        self.labelline.constant = 7;
    }else{
        self.labelline.constant = 23;
    }
    self.workPathLabel.text = self.recruitmentModel.district_name;
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[[NSString stringWithFormat:@"%@",self.recruitmentModel.end] intValue]];
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"YYYY/MM/dd"];
    NSString *  locationString = [formater stringFromDate:confromTimesp];
    self.timelabel.text = locationString;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUpTableViews
{
    [self.RecruitmentDeatailTable registerNib:[RecruitmentDetailCell nib] forCellReuseIdentifier:RecruitmentDetailCellID];
}

#pragma mark - Actions
-(IBAction)getContact:(id)sender
{
    if ([self.withchType isEqualToString:@"cityService"]) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.recruitmentModel._id,[self.recruitmentModel.category intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                if(![self.Consumptionbtn.titleLabel.text isEqualToString:@"消费积分查看联系方式"])
                {
                    NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                    NSString *telStr=[@"tel://" stringByAppendingString:string];
                    NSURL *url=[NSURL URLWithString:telStr];
                    [[UIApplication sharedApplication]openURL:url];
                    
                }else{
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否消耗积分查看联系方式" message:nil delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否",nil];
                    [alertView show];
                }
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }else{
    RIButtonItem *itemCancel = [RIButtonItem itemWithLabel:@"取消"];
    RIButtonItem *itemSubMit = [RIButtonItem itemWithLabel:@"确定" action:^{
        NSString *postUrl = [NSString stringWithFormat:@"%@%@",KinterFace_PublishInvalid,self.recruitmentModel._id];
        [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                [SVProgressHUD showSuccessWithStatus:@"信息作废成功"];
            }else{
                [SVProgressHUD showErrorWithStatus:@"信息作废失败"];
            }
        }];
    }];
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否作废该信息" message:nil cancelButtonItem:itemCancel otherButtonItems:itemSubMit, nil];
    [alertView show];
}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
    if (buttonIndex == 0) {
        NSString *postUrl = [NSString stringWithFormat:@"%@%@/%i",KinterFace_PublishContact,self.recruitmentModel._id,[self.recruitmentModel.category intValue]];
        [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
            if (success) {
                DLog(@"%@",response);
                NSString *string = [NSString stringWithFormat:@"%@",response[@"value"][@"contacts"][@"phone"]];
                
                NSString *title = [NSString stringWithFormat:@"%@:%@",self.recruitmentModel.contact,string];
                [self.Consumptionbtn setTitle:title forState:UIControlStateNormal];
            }else{
                [SVProgressHUD showErrorWithStatus:errMsg];
            }
        }];
    }
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *ss = [NSString stringWithFormat:@"%@",self.recruitmentModel.line];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 220, 25)];
    label.font = [UIFont systemFontOfSize:14];
    [label setLineBreakMode:NSLineBreakByWordWrapping];
    [label setNumberOfLines:0];
    NSString *_Detail = [NSString stringWithFormat:@"%@",ss];
    label.text = _Detail;
    [label sizeToFit];
    float labelheight = label.frame.size.height;
    return [RecruitmentDetailCell heightOfTableCell] - (90 - labelheight);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecruitmentDetailCell *RecruitmentDetailCell = [tableView dequeueReusableCellWithIdentifier:RecruitmentDetailCellID];
    RecruitmentDetailCell.contentlabel.text = [NSString stringWithFormat:@"%@",self.recruitmentModel.line];
    RecruitmentDetailCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return RecruitmentDetailCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
